import 'dart:async';
import 'dart:convert';

import 'package:ac/model/ac_blogger_model.dart';
import 'package:ac/model/ac_video_model.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../model/ac_login_model.dart';

class AcAppPreferences {
  static final AcAppPreferences _instance = AcAppPreferences._();
  late SharedPreferences _prefs;
  Completer initCompleter = Completer();

  static AcAppPreferences get instance => _instance;
  static const String _keyLoginUserModel = 'login_user_model';

  factory AcAppPreferences() {
    return _instance;
  }

  AcAppPreferences._();

  Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
    initCompleter.complete();
  }

  // 获取是否显示过引导页标志
  bool getGuideFlag() {
    return _prefs.getBool('guideFlag') ?? false;
  }

  // 设置是否显示过引导页标志
  Future<void> setGuideFlag(bool value) async {
    await _prefs.setBool('guideFlag', value);
  }

  // 保存用户登录信息
  Future<void> setUserModel(AcLoginModel? model) async {
    if (model == null) {
      await _prefs.remove(_keyLoginUserModel);
    } else {
      await _prefs.setString(_keyLoginUserModel, jsonEncode(model.toJson()));
    }
  }

  AcLoginModel? getLoginUserModel() {
    var modelStr = _prefs.getString(_keyLoginUserModel);
    if (modelStr == null || modelStr.isEmpty) {
      return null;
    }
    return AcLoginModel.fromJson(jsonDecode(modelStr));
  }

  void setBloggerList(List<AcBloggerModel>? list, String key) {
    if (list == null || list.isEmpty) {
      _prefs.remove('${key}_bloggerList');
      return;
    }
    _prefs.setString('${key}_bloggerList', jsonEncode(list));
  }

  List<AcBloggerModel> getBloggerList(String key) {
    var bloggerListStr = _prefs.getString('${key}_bloggerList');
    if (bloggerListStr == null || bloggerListStr.isEmpty) {
      return [];
    }
    List list = jsonDecode(bloggerListStr);
    List<AcBloggerModel> bloggerList = [];
    for (var item in list) {
      bloggerList.add(AcBloggerModel.fromJson(item));
    }
    return bloggerList;
  }

  void setVideoList(List<AcVideoModel>? list, String key) {
    if (list == null || list.isEmpty) {
      _prefs.remove('${key}_videoList');
      return;
    }
    _prefs.setString('${key}_videoList', jsonEncode(list));
  }

  List<AcVideoModel> getVideoList(String key) {
    var videoListStr = _prefs.getString('${key}_videoList');
    if (videoListStr == null || videoListStr.isEmpty) {
      return [];
    }
    List list = jsonDecode(videoListStr);
    List<AcVideoModel> videoList = [];
    for (var item in list) {
      videoList.add(AcVideoModel.fromJson(item));
    }
    return videoList;
  }

  void addPublishPost(String str, String userId) {
    var posts = _prefs.getStringList("${userId}_publishPost");
    posts ??= [];
    posts.add(str);
    _prefs.setStringList("${userId}_publishPost", posts);
  }

  List<Post> getPublishPost(String userId) {
    var posts = _prefs.getStringList("${userId}_publishPost");
    posts ??= [];
    return posts.map((e) => Post.fromJson(jsonDecode(e))).toList();
  }

  // 删除帖子
  void removePublishPost(String userId, String post) {
    var posts = _prefs.getStringList("${userId}_publishPost");
    posts ??= [];
    posts.removeWhere((element) => element == post);
    _prefs.setStringList("${userId}_publishPost", posts);
  }

  void updatePublishPost(String oldJson, String newJson, String userId) {
    var posts = _prefs.getStringList("${userId}_publishPost");
    posts ??= [];
    for (var value in posts) {
      if (value == oldJson) {
        var index = posts.indexOf(value);
        debugPrint('updatePublishPost: $index');
        posts.replaceRange(index, index + 1, [newJson]);
        _prefs.setStringList("${userId}_publishPost", posts);
        return;
      }
    }
  }

  void setAutoTranslate(bool value) {
    _prefs.setBool('autoTranslate', value);
  }

  bool getAutoTranslate() {
    return _prefs.getBool('autoTranslate') ?? false;
  }

  void setTranslateRecord(String key, String value) {
    var mapStr = _prefs.getString('translateRecord');
    if (mapStr == null || mapStr.isEmpty) {
      _prefs.setString('translateRecord', jsonEncode({key: value}));
      return;
    } else {
      Map map = jsonDecode(mapStr);
      map[key] = value;
      _prefs.setString('translateRecord', jsonEncode(map));
    }
  }

  String? getTranslateRecord(String key) {
    var mapStr = _prefs.getString('translateRecord');
    if (mapStr == null || mapStr.isEmpty) {
      return null;
    } else {
      Map map = jsonDecode(mapStr);
      return map[key];
    }
  }
}
